package com.atom.module.lib.opengles.egl

import android.opengl.EGL14
import android.opengl.EGLExt

class EGLConfigAttrs {

    companion object {
        private const val EGL_RECORDABLE_ANDROID = 0x3142 // Android-specific extension.
    }

    private var version: Int = 2
    private var red = 8
    private var green = 8
    private var blue = 8
    private var alpha = 8
    private var depth = 8
    private var surfaceType = EGL14.EGL_WINDOW_BIT
    private var renderableType = EGL14.EGL_OPENGL_ES2_BIT
    private var recordableType = false

    fun red(red: Int): EGLConfigAttrs {
        this.red = red
        return this
    }

    fun green(green: Int): EGLConfigAttrs {
        this.green = green
        return this
    }

    fun blue(blue: Int): EGLConfigAttrs {
        this.blue = blue
        return this
    }

    fun alpha(alpha: Int): EGLConfigAttrs {
        this.alpha = alpha
        return this
    }

    fun depth(depth: Int): EGLConfigAttrs {
        this.depth = depth
        return this
    }

    fun renderableType(type: Int): EGLConfigAttrs {
        renderableType = type
        return this
    }

    fun surfaceType(type: Int): EGLConfigAttrs {
        surfaceType = type
        return this
    }

    fun version(version: Int): EGLConfigAttrs {
        renderableType(
            if (version >= 3) {
                EGL14.EGL_OPENGL_ES2_BIT or EGLExt.EGL_OPENGL_ES3_BIT_KHR
            } else {
                EGL14.EGL_OPENGL_ES2_BIT
            }
        )
        this.version = version
        return this
    }

    fun recordableType(record: Boolean): EGLConfigAttrs {
        recordableType = record
        return this
    }


    fun build(): IntArray {
        return intArrayOf(
            EGL14.EGL_SURFACE_TYPE,
            surfaceType,  //渲染类型
            EGL14.EGL_RED_SIZE,
            red,  //指定RGB中的R大小（bits）
            EGL14.EGL_GREEN_SIZE,
            green,  //指定G大小
            EGL14.EGL_BLUE_SIZE,
            blue,  //指定B大小
            EGL14.EGL_ALPHA_SIZE,
            alpha,  //指定Alpha大小，以上四项实际上指定了像素格式
            EGL14.EGL_DEPTH_SIZE,
            depth,  //指定深度缓存(Z Buffer)大小
            EGL14.EGL_RENDERABLE_TYPE,
            renderableType,  //指定渲染api类别, 如上一小节描述，这里或者是硬编码的4(EGL14.EGL_OPENGL_ES2_BIT)
            if (recordableType) EGL_RECORDABLE_ANDROID else EGL14.EGL_NONE,
            if (recordableType) 1 else 0,
            EGL14.EGL_NONE
        ) //总是以EGL14.EGL_NONE结尾
    }

    override fun toString(): String {
        return build().contentToString()
    }
}